/**
 * Created by renzhiqiang on 17/5/30.
 */
var http=require("http");
var url=require("url");
var config = require('./config/config');
var server=http.createServer(function(sreq,sres){
    sres.setHeader('Access-Control-Allow-Origin', '*');
    sreq.setEncoding('utf8');
    var chunks = '';
    var opts = {};
    opts = config(sreq);
    if('POST' == sreq.method) {
        sreq.on('data', function(chunk){
            opts = config(sreq);
            chunks = chunk;
            var creq=http.request(opts, function (cres) {
                sres.writeHead(cres.statusCode,cres.headers);
                cres.pipe(sres);
            }).on('error', function (e) {
                console.log("错误:" + e.message);
            });
        });
    } else {
        var creq=http.request(opts, function (cres) {
            sres.writeHead(cres.statusCode,cres.headers);
            cres.pipe(sres);
        }).on('error', function (e) {
            console.log("错误:" + e.message);
        });
    }
    sreq.pipe(creq);
    creq.end();
});
server.listen(8060,"127.0.0.1", function () {
    console.log("开始监听"+server.address().port+"......");
});

process.on('uncaughtException',function (e) {
    console.info(e);
});